<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>选择str</title>
</head>

<body>
    <form action="">
        <p><input type="checkbox">约翰尼</p>
        <p><input type="checkbox">约翰尼</p>
        <p><input type="checkbox">约翰尼</p>
        <p><input type="checkbox">约翰尼</p>
    </form>
    <button onclick="getAll(0)">全选</button>
    <button onclick="getAll(1)">全不选</button>
    <button onclick="getAll(2)">反选</button>
</body>
<script>
    let btn = document.getElementsByTagName("button")
    let inputs = document.getElementsByTagName("input")

    // btn[0].onclick = () => {
    //     for (let i = 0; i < inputs.length; i++) {
    //         inputs[i].checked = true
    //     }
    // }

    // btn[1].onclick = () => {
    //     for (let i = 0; i < inputs.length; i++) {
    //         inputs[i].checked = false
    //     }
    // }

    // btn[2].onclick = () => {
    //     for (let i = 0; i < inputs.length; i++) {
    //         inputs[i].checked = !inputs[i].checked
    //     }
    // }

    // const getAll = (num) => {
    //     for (let i = 0; i < inputs.length; i++) {
    //         if (num == 0) {
    //             inputs[i].checked = true
    //         }
    //         else if (num == 1) {
    //             inputs[i].checked = false
    //         }
    //         else {
    //             inputs[i].checked = !inputs[i].checked
    //         }
    //     }
    // }

    const getAll = (num) => {
        for (let index = 0; index < inputs.length; index++) {
            if (num == 0) {
                inputs[index].checked = true
            }
            else if (num == 1) {
                inputs[index].checked = false
            }
            else {
                inputs[index].checked = !inputs[index].checked
            }
        }
    }
</script>

</html>